package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.itheima.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author ZJ
 */
@CanalEventListener
public class BusinessListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @ListenPoint(schema = "changgou_business",table = "tb_ad")
    public void adupdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        System.out.println("广告表数据发生改变");
        /*//改变之前
        rowData.getBeforeColumnsList().forEach(
                column -> System.out.println("改变前的数据："+column.getName()+"::"+column.getValue()));
        //改变之后
        rowData.getAfterColumnsList().forEach(
                column -> System.out.println("改变后的数据："+column.getName()+"::"+column.getValue()));*/

        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            //只需要名为position的value
            if("position".equals(column.getName())){
                System.out.println("发送最新数据到MQ:"+column.getValue());
                rabbitTemplate.convertAndSend("", RabbitMQConfig.AD_UPDATE_QUEUE,column.getValue());
            }
        }


    }


}
